home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 7562 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.5 KB  |  104 lines

  1. Path: news.Gsu.EDU!gs01lmh
  2. From: gs01lmh@panther.Gsu.EDU (Louis Miller Hall)
  3. Newsgroups: comp.lang.c++
  4. Subject: Something Funny Going On Here?!?
  5. Date: 24 Feb 1996 01:02:25 GMT
  6. Organization: Georgia State University
  7. Message-ID: <4glo31$boc@sphinx.Gsu.EDU>
  8. NNTP-Posting-Host: panther.gsu.edu
  9. NNTP-Posting-User: gs01lmh
  10. X-Newsreader: TIN [version 1.2 PL2]
  11.  
  12.  
  13.  
  14. I was wondering if anyone can help me here, no matter what data is 
  15. entered, I cannot get the array h[i] to get any value other than 0 in any 
  16. ith position, which causes me to get a divide by 0 on the next line, I 
  17. have copied this algorithm directly from a textbook, and have traced the 
  18. problem line by line and have found that the error occurs here, but I 
  19. cannot understand why, any help or insight would be appreciated.
  20.  
  21. Thanks
  22.  
  23. Louis
  24.  
  25. I have eliminated non-essential code:
  26.  
  27.  
  28.  
  29.  
  30. #define  MAX_ENTRIES   10
  31. #define  MAXCHARS       81
  32. #include <fstream.h>
  33.  
  34. int        i,
  35.             j,                        
  36. //  loop control variables
  37.             n;                       // number 
  38. of data points that are given
  39.  
  40. double   x[MAX_ENTRIES],    // the x0...xn's that are given in the data
  41.             a[MAX_ENTRIES],    // the values of f(x)'s, defined 
  42. in the algorithm
  43.             b[MAX_ENTRIES],
  44.             c[MAX_ENTRIES],
  45.             d[MAX_ENTRIES],    // what will be the coeefficients 
  46. of the cubic spline
  47.             l[MAX_ENTRIES],
  48.             u[MAX_ENTRIES],
  49.             alpha[MAX_ENTRIES];
  50.             z[MAX_ENTRIES],
  51.             h[MAX_ENTRIES];    // intermediate caluclation variables
  52.  
  53.  
  54.  
  55. void main ()
  56.   {  // main
  57.   // get all input data in input step of algorithm
  58.   cout << "This program will develop the cubic spline interpolant S for 
  59. the "
  60.          << "function f " << endl << "defined at the numbers 
  61. x[0]...x[n]" << endl
  62.          << endl;
  63.  
  64.  
  65.   // begin loop to continue getting data until the user is finished
  66.  
  67.   while (1)
  68.      {  // while
  69.  
  70.      cout << endl << "How many data points 0 to quit>";
  71.      cin  >> n;
  72.     if (n==0) break;
  73.  
  74.  
  75.      cout << endl << "Enter the x[n] values:" << endl;
  76.      for (i=0; i < n; i++)
  77.         { // for
  78.         cout <<  "x[" << i << "] = ";
  79.         cin >> x[i];
  80.         } // for
  81.  
  82.      cout << endl << "Now enter the f(x) values:" << endl;
  83.  
  84.      // will display f(actual x) and will directly put that into a[x] 
  85. per alg.
  86.      for (i=0; i<n; i++)
  87.         { // for
  88.         cout <<  "f[" << x[i] << "] = ";
  89.         cin >> a[i];
  90.         } // for
  91.  
  92.      // Step 1
  93.  
  94. //________________________________________________________
  95. // I CANNOT GET ANY h[i] TO GET A VALUE OTHER THAN 0 HERE??!!?? 
  96.      for (i=0; i<n-1; i++)
  97.         h[i] = (x[i+1] - x[i]);
  98. //________________________________________________________
  99.      // step 2
  100.      for (i=1; i<=n-1; i++)
  101.         alpha[i] = ((3/h[i])*(a[i+1] - a[i]))-((3/h[i-1])*(a[i]-a[i-1]));
  102.  
  103. // AND THE DIVIDE BY ZERO OCCURRS HERE
  104.